package com.ruitu.mall_98_1.product.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beanu.arad.base.BaseFragment;
import com.beanu.arad.base.my_base.base_list.ListBaseAdapter;
import com.beanu.arad.base.my_base.base_list.SuperViewHolder;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.product.activity.ProductListBySearchActivity;
import com.ruitu.router_module.bean.product.Category;
import com.ruitu.router_module.bean.product.SearchCondition;

import java.util.List;

/**
 * Created by Administrator on 2018/5/8.
 */
public class CategoryDetailFragment extends BaseFragment {
    private RecyclerView rcv_category;
    private TextView tv_empty;//空白布局

    private Category category;//一级分类对象
    private List<Category> categoryList;//二级分类列表
    private Category2Adapter adapter;

    public static CategoryDetailFragment newInstance(Category category) {
        Bundle args = new Bundle();
        CategoryDetailFragment fragment = new CategoryDetailFragment();
        args.putSerializable("category", category);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        category = (Category) getArguments().getSerializable("category");
        categoryList = category.getNodes();

        adapter = new Category2Adapter(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_category_detail, container, false);
        rcv_category = v.findViewById(R.id.rcv_category);
        tv_empty = v.findViewById(R.id.tv_empty);

        if (isDataEmpty(categoryList)) {//二级分类为空
            tv_empty.setVisibility(View.VISIBLE);
            rcv_category.setVisibility(View.GONE);
        } else {
            tv_empty.setVisibility(View.GONE);
            rcv_category.setVisibility(View.VISIBLE);
        }

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_category.setLayoutManager(manager);
        rcv_category.setAdapter(adapter);
        adapter.setDataList(categoryList);

        return v;
    }

    private class Category2Adapter extends ListBaseAdapter<Category> {

        public Category2Adapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.pro_item_category_2;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            final Category category2 = categoryList.get(position);
            TextView tv_name = holder.getView(R.id.tv_name);
            tv_name.setText(category2.getTypeName());

            RecyclerView rcv_category_3 = holder.getView(R.id.rcv_category_3);
            Category3Adapter adapter3 = new Category3Adapter(getActivity());
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
            rcv_category_3.setLayoutManager(manager);
            rcv_category_3.setAdapter(adapter3);
            final List<Category> categoryList3 = category2.getNodes();
            adapter3.setDataList(categoryList3);//设置三级数据
            adapter3.setOnItemClickListener(new OnItemClickListener() {//点击第三级分类
                @Override
                public void onItemClick(View itemView, int position) {
                    Category category3 = categoryList3.get(position);
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("category_1_id", category.getId());//一级分类id
//                    bundle.putSerializable("category", category3);//三级分类对象
//                    bundle.putSerializable("categoryList", (ArrayList) categoryList);
//                    startActivity(ProductListActivity.class, bundle);//跳转到商品列表页面

                    Bundle bundle = new Bundle();
                    SearchCondition condition = new SearchCondition();
                    condition.setClassA(category.getId());
                    condition.setClassB(category2.getId());
                    condition.setClassC(category3.getId());
                    //AppHolder.instance().getCITY().getName()
                    condition.setCityName("");
                    bundle.putSerializable("condition", condition);
                    startActivity(ProductListBySearchActivity.class, bundle);
                }
            });
        }

        private class Category3Adapter extends ListBaseAdapter<Category> {

            public Category3Adapter(Context context) {
                super(context);
            }

            @Override
            public int getLayoutId() {
                return R.layout.pro_item_category_3;
            }

            @Override
            public void onBindItemHolder(SuperViewHolder holder, int position) {
                Category category3 = mDataList.get(position);
                TextView tv_name = holder.getView(R.id.tv_name);
                tv_name.setText(category3.getTypeName());
            }
        }
    }
}
